//////////////////////////////////////////////////////////////////////////////////////////////////
//	File: MessagingSystem.cpp
//
//	Author: Justin Maza
//
//	Purpose: Handles the communication between game objects via messages
//			 
//////////////////////////////////////////////////////////////////////////////////////////////////

#include "MessagingSystem.h"
MessagingSystem* MessagingSystem::pInstance = nullptr;

MessagingSystem::MessagingSystem(void)
{
}


MessagingSystem::~MessagingSystem(void)
{
}

MessagingSystem* MessagingSystem::GetInstance(void)
{
	if(pInstance == nullptr)
		pInstance = new MessagingSystem;

	return pInstance;
}

void MessagingSystem::DeleteInstance(void)
{
	if(pInstance != nullptr)
	{
		delete pInstance;
		pInstance = nullptr;
	}
}

void MessagingSystem::InitMessageSystem(MESSENGER fpMsg)
{
	if(!fpMsg)
		return;
	
	fpMsger = fpMsg;
}

void MessagingSystem::SendMsg(BaseMessage* pMsg)
{
	if(!pMsg)
		return;

	q_MsgQueue.push(pMsg);
}

void MessagingSystem::ProcessMessages(void)
{
	if(!fpMsger)
		return;

	while(!q_MsgQueue.empty())
	{
		fpMsger(q_MsgQueue.front());
		delete q_MsgQueue.front();
		q_MsgQueue.pop();
	}
}

void MessagingSystem::ClearMessages(void)
{
	while(!q_MsgQueue.empty())
	{
		delete q_MsgQueue.front();
		q_MsgQueue.pop();
	}
}

void MessagingSystem::ShutdownMsgSystem(void)
{
	ClearMessages();
	fpMsger = nullptr;
}